package research_aggregator;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class CapabilityList {
	public Document CList = null;
	
	public CapabilityList () {
		SAXBuilder builder = new SAXBuilder();
		try {
			CList = builder.build(new ByteArrayInputStream("<cl></cl>".getBytes()));
		}catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public CapabilityList (String xml) {
		SAXBuilder builder = new SAXBuilder();
		try {
			CList =
				builder.build(new ByteArrayInputStream(xml.getBytes()));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public CapabilityList addCapability(String item, String lowbound, String upperbound) {
		Element root = CList.getRootElement();
		Element newItem = new Element(item);
		if (lowbound != null) newItem.setAttribute("low", lowbound);
		if (upperbound != null) newItem.setAttribute("upper", upperbound);
		root.addContent(newItem);
		return this;
	}	
	
	public String toXml()
	{
		XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
		return outputter.outputString(CList).replaceAll("[\n\r]", "");
	}
}
